import axios from 'axios'
import { ElMessage } from "element-plus";

// 创建axios实例
const request = axios.create({
    //基础url
    baseURL: '/api',
    //请求超时时间
    timeout: 10000,
    //设置请求头
    headers: {
        'Content-Type': 'application/json'
    }
})

// 请求拦截器
request.interceptors.request.use(
    config => {
        // 判断是否存在token
        const token = localStorage.getItem('adminToken')

        // 存在token，就添加进请求头中
        if (token){
            config.headers.Authorization = token
        }
        return config
    },
    error => {
        //处理请求错误
        console.error('Request error:',error)
        return Promise.reject(error)
    }
)

// 响应拦截器
request.interceptors.response.use(
    response => {
        // 处理响应数据
        return response.data
    },
    error => {
        if(error.response.status){
            switch (error.response.status){
                case 401:
                    // 未授予权，清除token并跳转到登录页面
                    localStorage.removeItem('adminToken')
                    localStorage.removeItem('adminName')
                    // window.location.href = '/login'
                    ElMessage.error('登录已过期，请重新登录')
                    break;

                case 403:
                    ElMessage.error('无权限访问')
                    break;

                case 404:
                    ElMessage.error('请求的资源不存在')
                    break;

                case 500:
                    ElMessage.error('服务器错误')
                    break;

                default:
                    ElMessage.error('未知错误')
            }
        }else {
            ElMessage.error('网络连接错误')
        }
        return Promise.reject(error)
    }
)

//导出api相关接口对象
export default {
    //管理员相关接口
    admin:{
        //登录接口
        login(data) {
            return request({
                url: '/admin/login/status',
                method: 'post',
                data
            })
        },
        //注销接口
        logout() {
            return request({
                url: '/admin/logout',
                method: 'post'
            })
        }
    },

    consumer: {

        register(data){
            return request({
                url: 'consumer/register',
                method: 'post',
                data
            })
        },

        delete(id){
            return request({
                url: 'consumer/deleteConsumer',
                method: 'get',
                params:{ consumerId: id }
            })
        },

        allConsumer(){
            return request({
                url: 'consumer/allConsumer',
                method: 'get'
            })
        },

        search( keyword){
            return request({
                url: 'consumer/consumerOfNameLike',
                method: 'get',
                params:{ consumerName: keyword }
            })
        },

        numberOfConsumer() {
            return request({
                url: '/consumer/numberOfConsumer',
                method: 'get'
            })
        },

        update(data){
            return request({
                url: 'consumer/updateConsumerInfo',
                method: 'post',
                data
            })
        }
    },

    singer:{
        add(data){
            return request({
                url: 'singer/add',
                method: 'post',
                data
            })
        },

        delete(id){
            return request({
                url: 'singer/delete',
                method: 'get',
                params:{ id }
            })
        },

        update(data){
            return request({
                url: 'singer/update',
                method: 'post',
                data
            })
        },

        updateAvatar(formData){
            return request({
                url: 'singer/singerOfPic',
                method: 'post',
                data: formData,
                headers: {
                    'Content-Type': 'multipart/form-data'
                }
            })
        },

        getAll(){
            return request({
                url: 'singer/allSinger',
                method: 'get'
            })
        },

        search(keyword){
            return request({
                url: 'singer/singerOfName',
                method: 'get',
                params:{ singerName: keyword }
            })
        },

        getBySex(sex){
            return request({
                url: 'singer/singerOfSex',
                method: 'get',
                params:{ singerSex: sex }
            })
        },

        getById(id){
            return request({
                url: 'singer/singerOfId',
                method: 'get',
                params:{ singerId: id }
            })
        },

        numberOfSinger(){
            return request({
                url: 'singer/numberOfSinger',
                method: 'get'
            })
        },

        numberOfSingerBySex(sex){
            return request({
                url: 'singer/numberOfSingerBySex',
                method: 'get',
                params: {sex}
            })
        },

        getByFirstLetter(firstLetter) {
            return request({
                url: '/singer/singerOfFirstLetter',
                method: 'get',
                params: { firstLetter }
            })
        }
    },

    song:{
        numberOfSong(){
            return request({
                url: 'song/numberOfSong',
                method: 'get'
            })
        },

        getAll() {
            return request({
                url: 'song/allSong',
                method: 'get'
            })
        },

        add(formData){
            return request({
                url: 'song/add',
                method: 'post',
                data: formData,
                headers: {
                    'Content-Type': 'multipart/form-data'
                }
            })
        },

        delete(id){
            return request({
                url: 'song/delete',
                method: 'get',
                params:{ songId: id }
            })
        },

        update(data){
            return request({
                url: 'song/update',
                method: 'post',
                data
            })
        },

        updatePic(formData){
            return request({
                url: 'song/songOfPic',
                method: 'post',
                data: formData,
                headers: {
                    'Content-Type': 'multipart/form-data'
                }
            })
        },

        search(keyword){
            return request({
                url: 'song/songOfName',
                method: 'get',
                params:{ songName: keyword }
            })
        },

        getById(id){
            return request({
                url: 'song/songOfId',
                method: 'get',
                params:{ songId: id }
            })
        },

        getBySingerId(singerId){
            return request({
                url: 'song/songOfSingerId',
                method: 'get',
                params: { singerId }
            })
        },
    },

    rank:{
        numberOfRank(){
            return request({
                url: 'rank/numberOfRank',
                method: 'get'
            })
        },

        getAll() {
            return request({
                url: 'rank/allRank',
                method: 'get'
            })
        },

        add(data){
            return request({
                url: 'rank/add',
                method: 'post',
                data
            })
        },

        delete(id){
            return request({
                url: 'rank/delete',
                method: 'get',
                params:{ rankId: id }
            })
        },

        update(data){
            return request({
                url: 'rank/update',
                method: 'post',
                data
            })
        },

        getById(id){
            return request({
                url: 'rank/rankOfId',
                method: 'get',
                params:{ rankId: id }
            })
        },

        search(keyword){
            return request({
                url: 'rank/likeRankOfTitle',
                method: 'get',
                params:{ rankTitle: keyword }
            })
        },

        updateAvatar(formData){
            return request({
                url: 'rank/rankOfPic',
                method: 'post',
                data: formData,
                headers: {
                    'Content-Type': 'multipart/form-data'
                }
            })
        }
    },

    rankSong:{
        numberOfRankSong(){
            return request({
                url: 'rankSong/numberOfRankSong',
                method: 'get'
            })
        },

        add(data){
            return request({
                url: 'rankSong/insertRankSong',
                method: 'post',
                data
            })
        },

        delete(id){
            return request({
                url: 'rankSong/deleteRankSong',
                method: 'post',
                params:{ rankSongId: id }
            })
        },

        update(data){
            return request({
                url: 'rankSong/updateRankSong',
                method: 'post',
                data
            })
        },

        deleteByRankAndSong(data) {
            return request({
                url: '/rankSong/deleteRankSongByRankIdAndRankSongId',
                method: 'get',
                params: data
            })
        },

        getByRankId(rankId){
            return request({
                url: 'rankSong/rankSongByRankId',
                method: 'get',
                params: {rankId}
            })
        }
    }
}